/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: jian yi, eyjian@qq.com
 */
#include <iostream>
 
void remove_last(std::string& str, char c)
{
        std::string::size_type pos = str.rfind(c);
        if (pos+1 == str.length())
                str.erase(pos);
}
 
void remove_last(std::string& str, const std::string& sep)
{
        std::string::size_type pos = str.rfind(sep);
        if (pos != std::string::npos)
                str.erase(pos);
}
 
int main()
{
        std::string str1 = "/a/b/c/d";
        std::string str2 = "/a/b/c/d/";
        std::cout << str1 << std::endl << str2 << std::endl;
 
        remove_last(str1, '/');
        remove_last(str2, '/');
        std::cout << str1 << std::endl << str2 << std::endl;
        std::cout << std::endl;
 
        const char* sep = "/bin/";
        std::string str3 = "/tmp/run/bin/jws";
        std::string str4 = "/tmp/run/binx/jws";
        std::cout << str3 << std::endl << str4 << std::endl;
 
        remove_last(str3, sep);
        remove_last(str4, sep);
        std::cout << str3 << std::endl << str4 << std::endl;
 
        return 0;
}